/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.BanHang;

import dao.daoKhuyenMai;
import gui.customcbb;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Nhanvien;
import pojo.Sanpham;

/**
 *
 * @author Cong
 */
public class QLKhuyenMai extends myclass.MyAdminFrame {

    /**
     * Creates new form QLKhuyenMai1
     */
    public static String TENKHUVUC = "Quản Lý Khuyến Mãi";
    private static final String columnskm[] = {"Mã chương trình", "Tên chương trình", "Ngày bắt đầu", "Ngày kết thúc"};
    private static final String columnssp[] = {"ID", "Mã SP", "Tên SP", "Loại"};
    private DefaultTableModel tbModelkm;
    private DefaultTableModel tbModelsp;
    private DefaultComboBoxModel cbbModelsp;
    private static SimpleDateFormat fm;

    public QLKhuyenMai(Nhanvien nv) {
        super(nv);
        initComponents();

        fm = new SimpleDateFormat("dd/MM/yyyy");
        tbModelkm = new DefaultTableModel();
        tbModelsp = new DefaultTableModel();
        cbbModelsp = new DefaultComboBoxModel();
        tbModelkm.setColumnIdentifiers(columnskm);
        tbModelsp.setColumnIdentifiers(columnssp);
        jtbct.setModel(tbModelkm);
        jtbsp4.setModel(tbModelsp);
        jcbbsp.setModel(cbbModelsp);
        LoadTBKM();
        LoadCBB();
    }

    private void LoadTBKM() {
        tbModelkm.getDataVector().clear();
        List<pojo.Khuyenmai> ds = dao.daoKhuyenMai.LaydsKM();
        if (!ds.isEmpty()) {
            for (int i = 0; i < ds.size(); i++) {
                pojo.Khuyenmai km = ds.get(i);
                tbModelkm.addRow(new Object[]{km.getId(), km.getTenchuongtrinh(),
                            fm.format(km.getNgaybatdau()), fm.format(km.getNgayketthuc())});
            }
        }
    }

    private void LoadCBB() {
        List<Sanpham> ds = daoKhuyenMai.GetdsSP();
        cbbModelsp.removeAllElements();
        for (int i = 0; i < ds.size(); i++) {
            cbbModelsp.addElement(new customcbb(ds.get(i).getId(), ds.get(i).getTensanpham()));
        }
    }

    private void LoadTBSP(pojo.Khuyenmai km) {
        tbModelsp.setRowCount(0);
        List<Object[]> ds = dao.daoKhuyenMai.GetSPKM(km);
        if (!ds.isEmpty()) {
            for (int i = 0; i < ds.size(); i++) {
                Object[] sp = ds.get(i);
                tbModelsp.addRow(sp);
            }
        }
    }

    private void ClearDT() {
        jlbmact.setText("");
        jtfngaybd.setText("");
        jtfngaykt.setText("");
        jtftenct.setText("");
        tbModelsp.setRowCount(0);
    }

    private boolean CheckField() {
        if ("".equals(jtftenct.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập tên chương trình.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ("".equals(jtfngaybd.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập ngày bắt đầu.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ("".equals(jtfngaykt.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập ngày kết thúc.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    private boolean CheckFieldSP() {
        if ("".equals(jlbmact.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn chương trình cần thêm.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ("".equals(jtfloai.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập loại khuyến mãi.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        try {
            Long.parseLong(jtfloai.getText());
        } catch (Exception pe) {
            JOptionPane.showMessageDialog(this, "Loại phải là số nguyên không âm.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbct = new javax.swing.JTable();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jtbsp4 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtftenct = new javax.swing.JTextField();
        jtfngaybd = new javax.swing.JTextField();
        jtfngaykt = new javax.swing.JTextField();
        jlbmact = new javax.swing.JLabel();
        jbttimkiem = new javax.swing.JButton();
        jbtreset = new javax.swing.JButton();
        jtbthem = new javax.swing.JButton();
        jbtxoa = new javax.swing.JButton();
        jbtluu = new javax.swing.JButton();
        jbtthoat = new javax.swing.JButton();
        jPanel11 = new javax.swing.JPanel();
        jtfloai = new javax.swing.JTextField();
        jcbbsp = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtbthemsp = new javax.swing.JButton();
        jbtxoasp = new javax.swing.JButton();
        jbtluusp = new javax.swing.JButton();
        jbtclearsp = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản lý chương trình khuyến mãi");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Quản lý chương trình khuyến mãi");

        jtbct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbctMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbct);

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin chương trình khuyến mãi"));

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết sản phẩm được khuyến mãi"));

        jtbsp4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbsp4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbsp4MouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(jtbsp4);

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel2.setText("Mã chương trình:");

        jLabel3.setText("Tên chương trình:");

        jLabel4.setText("Ngày bắt đầu:");

        jLabel5.setText("Ngày kết thúc:");

        jbttimkiem.setText("Tìm kiếm");
        jbttimkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbttimkiemActionPerformed(evt);
            }
        });

        jbtreset.setText("Nhập lại");
        jbtreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtresetActionPerformed(evt);
            }
        });

        jtbthem.setText("Thêm");
        jtbthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbthemActionPerformed(evt);
            }
        });

        jbtxoa.setText("Xóa");
        jbtxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtxoaActionPerformed(evt);
            }
        });

        jbtluu.setText("Lưu");
        jbtluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtluuActionPerformed(evt);
            }
        });

        jbtthoat.setText("Thoát");
        jbtthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtthoatActionPerformed(evt);
            }
        });

        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder("Quản lý sản phẩm"));

        jcbbsp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setText("Sản phẩm:");

        jLabel7.setText("Loại:");

        jtbthemsp.setText("Thêm");
        jtbthemsp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbthemspActionPerformed(evt);
            }
        });

        jbtxoasp.setText("Xóa");
        jbtxoasp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtxoaspActionPerformed(evt);
            }
        });

        jbtluusp.setText("Lưu");
        jbtluusp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtluuspActionPerformed(evt);
            }
        });

        jbtclearsp.setText("Nhập lại");
        jbtclearsp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtclearspActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel11Layout.createSequentialGroup()
                        .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbbsp, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtfloai))
                        .addContainerGap())
                    .addGroup(jPanel11Layout.createSequentialGroup()
                        .addGap(0, 7, Short.MAX_VALUE)
                        .addComponent(jtbthemsp, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtxoasp, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtluusp, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtclearsp, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4))))
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jcbbsp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfloai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtxoasp)
                    .addComponent(jtbthemsp)
                    .addComponent(jbtluusp)
                    .addComponent(jbtclearsp))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jlbmact)
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(jbttimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtreset, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jtftenct, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfngaybd, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfngaykt, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(jtbthem, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtxoa, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtluu, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jlbmact))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtftenct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfngaybd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jtfngaykt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbttimkiem)
                            .addComponent(jbtreset))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtxoa)
                            .addComponent(jtbthem)
                            .addComponent(jbtthoat)
                            .addComponent(jbtluu))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(181, 181, 181)
                        .addComponent(jLabel1)
                        .addGap(0, 137, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtbctMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbctMouseClicked
        int dong = jtbct.getSelectedRow();
        jlbmact.setText(jtbct.getValueAt(dong, 0).toString());
        jtftenct.setText(jtbct.getValueAt(dong, 1).toString());
        jtfngaybd.setText(jtbct.getValueAt(dong, 2).toString());
        jtfngaykt.setText(jtbct.getValueAt(dong, 3).toString());
        pojo.Khuyenmai km = new pojo.Khuyenmai();
        km.setId(Long.parseLong(jtbct.getValueAt(dong, 0).toString()));
        LoadTBSP(km);
    }//GEN-LAST:event_jtbctMouseClicked

    private void jtbsp4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbsp4MouseClicked
        int dong = jtbsp4.getSelectedRow();
        String sp = jtbsp4.getValueAt(dong, 2).toString();
        jtfloai.setText(jtbsp4.getValueAt(dong, 3).toString());
        for (int i = 0; i < jcbbsp.getItemCount(); i++) {
            if (((customcbb) jcbbsp.getItemAt(i)).getValue().equals(sp)) {
                jcbbsp.setSelectedIndex(i);
                break;
            }
        }
    }//GEN-LAST:event_jtbsp4MouseClicked

    private void jbttimkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbttimkiemActionPerformed
        pojo.Khuyenmai km = new pojo.Khuyenmai();
        km.setTenchuongtrinh(jtftenct.getText());
        if (!"".equals(jtfngaybd.getText())) {
            try {
                km.setNgaybatdau(fm.parse(jtfngaybd.getText()));
            } catch (ParseException ex) {
                System.err.println(ex);
            }
        } else {
            km.setNgaybatdau(null);
        }
        if (!"".equals(jtfngaykt.getText())) {
            try {
                km.setNgayketthuc(fm.parse(jtfngaykt.getText()));
            } catch (ParseException ex) {
                System.err.println(ex);
            }
        } else {
            km.setNgayketthuc(null);
        }
        List<pojo.Khuyenmai> ds = dao.daoKhuyenMai.FindKM(km);
        if (!ds.isEmpty()) {
            tbModelkm.getDataVector().clear();
            for (int i = 0; i < ds.size(); i++) {
                pojo.Khuyenmai kh1 = ds.get(i);
                tbModelkm.addRow(new Object[]{kh1.getId(), kh1.getTenchuongtrinh(),
                            fm.format(kh1.getNgaybatdau()), fm.format(kh1.getNgayketthuc())});
            }
        } else {
            JOptionPane.showMessageDialog(this, "Không tìm thấy.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbttimkiemActionPerformed

    private void jbtresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtresetActionPerformed
        ClearDT();
    }//GEN-LAST:event_jbtresetActionPerformed

    private void jtbthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbthemActionPerformed
        if (CheckField()) {
            pojo.Khuyenmai km = new pojo.Khuyenmai();
            km.setTenchuongtrinh(jtftenct.getText());
            try {
                km.setNgaybatdau(fm.parse(jtfngaybd.getText()));
            } catch (ParseException ex) {
                System.err.println(ex);
                JOptionPane.showMessageDialog(this, "Ngày bắt đầu không đúng định dạng (dd/MM/yyyy).", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
            try {
                km.setNgayketthuc(fm.parse(jtfngaykt.getText()));
            } catch (ParseException ex) {
                System.err.println(ex);
                JOptionPane.showMessageDialog(this, "Ngày kết thúc không đúng định dạng (dd/MM/yyyy).", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
            boolean kq = dao.daoKhuyenMai.ThemCT(km);
            if (kq == true) {
                LoadTBKM();
            } else {
                JOptionPane.showMessageDialog(this, "Thêm thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jtbthemActionPerformed

    private void jbtxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtxoaActionPerformed
        if ("".equals(jlbmact.getText())) {
            JOptionPane.showMessageDialog(this, "Hãy chọn một chương trình cần xóa trong bảng.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa không?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            pojo.Khuyenmai km = new pojo.Khuyenmai();
            km.setId(Long.parseLong(jlbmact.getText()));
            km = daoKhuyenMai.LayThongTinKM(km);
            boolean kq = dao.daoKhuyenMai.XoaCT(km);
            if (kq == true) {
                LoadTBKM();
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại. \nKhuyến mãi này đã có ràng buộc Chi tiết khuyến mãi.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtxoaActionPerformed

    private void jbtluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtluuActionPerformed
        if ("".equals(jlbmact.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn chương trình cần sửa.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        } else {
            if (CheckField()) {
                pojo.Khuyenmai km = new pojo.Khuyenmai();
                km.setId(Long.parseLong(jlbmact.getText()));
                km.setTenchuongtrinh(jtftenct.getText());
                try {
                    km.setNgaybatdau(fm.parse(jtfngaybd.getText()));
                } catch (ParseException ex) {
                    System.err.println(ex);
                    JOptionPane.showMessageDialog(this, "Ngày bắt đầu không đúng định dạng (dd/MM/yyyy).", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
                try {
                    km.setNgayketthuc(fm.parse(jtfngaykt.getText()));
                } catch (ParseException ex) {
                    System.err.println(ex);
                    JOptionPane.showMessageDialog(this, "Ngày kết thúc không đúng định dạng (dd/MM/yyyy).", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
                boolean kq = dao.daoKhuyenMai.LuuCT(km);
                if (kq == true) {
                    LoadTBKM();
                } else {
                    JOptionPane.showMessageDialog(this, "Cập nhật thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jbtluuActionPerformed

    private void jbtthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtthoatActionPerformed

    private void jtbthemspActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbthemspActionPerformed
        if (CheckFieldSP()) {
            pojo.Khuyenmai km = new pojo.Khuyenmai();
            km.setId(Long.parseLong(jlbmact.getText()));
            Sanpham sp = new Sanpham();
            sp.setId(((customcbb) jcbbsp.getSelectedItem()).getKey());
            pojo.Chitietkhuyenmai ctkm = new pojo.Chitietkhuyenmai();
            ctkm.setKhuyenmai(km);
            ctkm.setLoai(Long.parseLong(jtfloai.getText()));
            ctkm.setSanpham(sp);
            boolean kq = daoKhuyenMai.ThemCTKM(ctkm);
            if (kq == true) {
                km.setId(Long.parseLong(jlbmact.getText()));
                LoadTBSP(km);
            } else {
                JOptionPane.showMessageDialog(this, "Thêm thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jtbthemspActionPerformed

    private void jbtxoaspActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtxoaspActionPerformed
        if (jtbsp4.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Hãy chọn một chi tiết khuyến mãi cần xóa trong bảng.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa không?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            pojo.Chitietkhuyenmai ctkm = new pojo.Chitietkhuyenmai();
            ctkm.setId(Long.parseLong(jtbsp4.getValueAt(jtbsp4.getSelectedRow(), 0).toString()));
            ctkm = daoKhuyenMai.LayThongTinCTKM(ctkm);
            boolean kq = daoKhuyenMai.XoaCTKM(ctkm);
            if (kq) {
                pojo.Khuyenmai km = new pojo.Khuyenmai();
                km.setId(Long.parseLong(jlbmact.getText()));
                LoadTBSP(km);
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtxoaspActionPerformed

    private void jbtluuspActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtluuspActionPerformed
        if ("".equals(jtfloai.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập loại khuyến mãi.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            Long.parseLong(jtfloai.getText());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Loại phải là số nguyên không âm.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        pojo.Chitietkhuyenmai ctkm = new pojo.Chitietkhuyenmai();
        ctkm.setId(Long.parseLong(jtbsp4.getValueAt(jtbsp4.getSelectedRow(), 0).toString()));
        pojo.Khuyenmai km = new pojo.Khuyenmai();
        km.setId(Long.parseLong(jlbmact.getText()));
        Sanpham sp = new Sanpham();
        sp.setId(((customcbb) jcbbsp.getSelectedItem()).getKey());
        ctkm.setKhuyenmai(km);
        ctkm.setLoai(Long.parseLong(jtfloai.getText()));
        ctkm.setSanpham(sp);
        boolean kq = daoKhuyenMai.LuuCTKM(ctkm);
        if (kq == true) {
            km.setId(Long.parseLong(jlbmact.getText()));
            LoadTBSP(km);
        } else {
            JOptionPane.showMessageDialog(this, "Lưu thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtluuspActionPerformed

    private void jbtclearspActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtclearspActionPerformed
        tbModelsp.setRowCount(0);
    }//GEN-LAST:event_jbtclearspActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JButton jbtclearsp;
    private javax.swing.JButton jbtluu;
    private javax.swing.JButton jbtluusp;
    private javax.swing.JButton jbtreset;
    private javax.swing.JButton jbtthoat;
    private javax.swing.JButton jbttimkiem;
    private javax.swing.JButton jbtxoa;
    private javax.swing.JButton jbtxoasp;
    private javax.swing.JComboBox jcbbsp;
    private javax.swing.JLabel jlbmact;
    private javax.swing.JTable jtbct;
    private javax.swing.JTable jtbsp4;
    private javax.swing.JButton jtbthem;
    private javax.swing.JButton jtbthemsp;
    private javax.swing.JTextField jtfloai;
    private javax.swing.JTextField jtfngaybd;
    private javax.swing.JTextField jtfngaykt;
    private javax.swing.JTextField jtftenct;
    // End of variables declaration//GEN-END:variables
}
